# -*- coding: utf-8 -*-
#   __author__ = "Casey"     Email = 395280963qq.com
#   Data :2019/11/12  Python:3.7

import requests
from io import BytesIO
import re
from fontTools.ttLib import TTFont
from font import texts

class Dianping(object):
        """调用例子   实际情况还有多处位置加密
        此处只写了一处位置的加密方式， 其他的请自行拓展。
        """
        def __init__(self):
                """经过本人测试  referer参数对访问影响并不大。
                """
                self.headers = \
                    {
                            'referer': 'http://www.dianping.com/shop/97789651',
                            'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 '
                                          '(KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36',
                            'Cookie':'_lxsdk_cuid=16c5f745cedc8-04e0619cc47ef2-5a13331d-1fa400-16c5f745cedc8; ci=45; iuuid=66956202832EDBCEBB4B734FD23CD22C5DE0A8684BF4D8C9F6D2DC7AD08DA3F0; cityname=%E9%87%8D%E5%BA%86; _lxsdk=66956202832EDBCEBB4B734FD23CD22C5DE0A8684BF4D8C9F6D2DC7AD08DA3F0; OUTFOX_SEARCH_USER_ID_NCOO=1902232296.399432; webp=1; __utma=74597006.920750092.1569745789.1569745789.1569805245.2; __utmz=74597006.1569805245.2.2.utmcsr=baidu|utmccn=(organic)|utmcmd=organic|utmctr=%E7%BE%8E%E5%9B%A2; i_extend=C_b1E035598994785644256219986816052234551719_c0_e7717089357388196581_anull_o1_dhotelpoitagb_k1002Gimthomepagecategory120__xhotelhomepage__yselect__zday; wm_order_channel=default; utm_source=; _lx_utm=utm_source%3D',

                    }

        def prase(self,url):
                """
                :param  url:
                :return:
                """
                response = requests.get(url, headers=self.headers,allow_redirects=False)
                if response.status_code == 200:
                        print(response.status_code,response.url)
                        #  匹配  字体文件地址
                        css_url = re.search('//s3plus.meituan.net/v1/.*?\.css', response.text, re.S)
                        url = 'https:' + css_url.group()
                        q = requests.get(url, self.headers)
                        #  匹配  与加密字class属性对应的字体文件
                        font_url = re.search('hours.*?embedded-opentype.*?url\(\"(.*?)\"\)\;} \.hours', q.text, re.S)
                        url = 'https:' + font_url.group(1)
                        #  获取字体文件 以进制流的方式
                        hours_woff = requests.get(url, self.headers).content
                        f = TTFont(BytesIO(hours_woff))
                        f_add = f.getGlyphOrder()
                        #  匹配  第二套与加密字class属性对应的字体文件
                        font_url = re.search('shopdesc.*?embedded-opentype.*?url\(\"(.*?)\"\)\;} \.shopdesc', q.text, re.S)
                        url = 'https:' + font_url.group(1)
                        shopdesc_woff1 = requests.get(url, self.headers).content
                        f = TTFont(BytesIO(shopdesc_woff1))
                        f_num = f.getGlyphOrder()
                        #  匹配  需要解密的内容
                        ad = re.search('<span class="info-name">营业时间：</span> <span class="item">(.*?)</span>', response.text, re.S)
                        a = ad.group(1)
                        #  a1  匹配的是每一个字体编码    例如['&#xee27;', '', '&#xe4ad;', '至', '&#xee27;', '', '&#xe639;']
                        a1 = re.findall('>(.*?)<', a, re.S)
                        #  剔除空字符的存在
                        while '' in a1:
                            a1.remove('')
                        #  a2  匹配的是所有加了密的字体    例如['&#xee27;', '&#xe4ad;', '&#xee27;', '&#xe639;', '&#xf0d6;']
                        a2 = re.findall('<\w.*?\\">(.*?)</\w', a, re.S)
                        #  匹配  所有加密字体的class属性
                        a3 = re.findall('class=\\"(.*?)\\"', a, re.S)
                        #  通过enumerate内置函数 获取class属性等于shopdesc的位置
                        num = [i for i, v in enumerate(a3) if a3[i] == 'shopdesc']
                        for i in num:
                                fonta = texts[f_num.index('uni'+a2[i][3:-1])]
                                a1[a1.index(a2[i])] = fonta

                        #     此处是遍历后续没有替换的字体   然后一一与字体库对比
                        count = 0
                        for i in a1:
                                if i[:3] == '&#x':
                                        fonta = texts[f_add.index('uni'+i[3:-1])]
                                        a1[count] = fonta
                                        count += 1
                                else:
                                        count += 1
                        text = ''.join(a1)
                        print(text)
                else:
                        print('访问失败，被识别，请手动跟换cookie尝试！')

test = Dianping()
test.prase('http://www.dianping.com/shop/97789651')
















